<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
    :lock-scroll="false"
    :title="isUpdate?'修改市场':'添加市场'"
    :visible="visible"
    custom-class="ele-dialog-form"
    width="600px"
    @update:visible="updateVisible">

    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="82px">

      <el-form-item
        label="推广平台:"
        prop="platform">
        <el-select
          v-model="form.platform"
          class="ele-block"
          clearable
          placeholder="请选择推广平台">
          <el-option
            v-for="item in platformList"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>

      <el-form-item
        label="市场名称:"
        prop="name">
        <el-input
          v-model="form.name"
          :maxlength="20"
          clearable
          placeholder="请输入市场名称"/>
      </el-form-item>

      <el-form-item
        label="展示模式:"
        prop="model">
        <el-select
          v-model="form.model"
          class="ele-block"
          clearable
          placeholder="请选择展示模式">
          <el-option
            v-for="item in modelList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="排序:">
        <el-input-number v-model="form.sort" :max="9999" :min="1" class="ele-fluid ele-text-left"
                         controls-position="right"></el-input-number>
      </el-form-item>
      <el-form-item
        label="状态:">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="0"/>
        <el-tooltip
          content="选择关闭将无法展示微信"
          placement="top">
          <i class="el-icon-_question" style="vertical-align: middle;margin-left: 8px;"></i>
        </el-tooltip>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button
        @click="updateVisible(false)">取消
      </el-button>
      <el-button
        :loading="loading"
        type="primary"
        @click="save">确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'BazaarEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({
        status: 1,
        sort: 1,
      }, this.data),

      // 展示平台
      platformList: [{value: 1, label: '百度SEM竞价'}
        , {value: 2, label: '百度信息流'}
        , {value: 3, label: '巨量引擎'}
        , {value: 4, label: '搜狗推广'}
        , {value: 5, label: '卧龙推广'}
        , {value: 6, label: '360点睛'}
      ],
      // 展示模式
      modelList: [{value: 1, label: '均衡展示'}
        , {value: 2, label: '随机展示'}
        , {value: 3, label: '权重展示'}
        , {value: 4, label: '轮番展示'}
      ],
      // 表单验证规则
      rules: {
        name: [
          {
            required: true,
            type: 'string',
            trigger: 'blur',
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error('请输入推广市场'));
              }
              this.$http.get('/bazaar/query?param=' + value).then(res => {
                if (res.data.code !== 0 || !res.data.data.length) {
                  return callback();
                }
                if (this.isUpdate && res.data.data[0].name === this.data.name) {
                  return callback();
                }
                callback(new Error('市场名称已存在'));
              }).catch(() => {
                callback();
              });
            }
          }
        ],
        platform: [
          {required: true, message: '请选择推广平台', trigger: 'blur'}
        ],
        model: [
          {required: true, message: '请选择客服展示模式', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (Object.keys(this.data).length > 0) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        for (let i in this.form) {
          this.form[i] = null;
        }
        this.form.sort = 1;
        this.form.status = 1;
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 确定编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/bazaar/' + [this.isUpdate ? 'update' : 'create'], this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message({type: 'success', message: res.data.msg});
              if (!this.isUpdate) {
                for (let i in this.form) {
                  this.form[i] = null;
                }
                this.form.sort = 1;
                this.form.status = 1;
              }
              this.updateVisible(false);
              this.$emit('done', this.isUpdate);
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>
